package com.liaoban.app.ui.mine;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dalong.recordlib.RecordVideoUtils;
import com.liaoban.app.R;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.helper.ImageHelper;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BaseUploadPicActivity;
import com.liaoban.app.util.CommonUtils;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

public class VideoCertActivity extends BaseUploadPicActivity {

    @BindView(R.id.tv_photo)
    TextView tvPhoto;
    @BindView(R.id.iv_photo)
    ImageView ivPhoto;
    @BindView(R.id.tv_cert_record)
    TextView tvCertRecord;
    @BindView(R.id.iv_cert_record)
    ImageView ivCertRecord;

    private File mImgFile; //图片文件
    private File mVideoFile; //视频文件
    private Bitmap mVideoFirstBitmap;

    @Override
    protected String getNewTitle() {
        return "视频认证";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_video_cert;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @OnClick({R.id.tv_photo, R.id.iv_photo})
    public void photo() {
        showSheetDialog(false);
    }

    @OnClick({R.id.tv_cert_record, R.id.iv_cert_record})
    public void certRecord() {
        showSheetVideoDialog();
    }

    @Override
    protected void uploadPicSuccessful(boolean isTailor, File outFile, Uri filePath) {
        ImageHelper.load(ivPhoto, String.valueOf(outFile));
        tvPhoto.setVisibility(View.GONE);
        mImgFile = outFile;
    }

    @Override
    protected void uploadVideoSuccessful(String videoPath) {
        super.uploadVideoSuccessful(videoPath);
        tvCertRecord.setVisibility(View.GONE);
        mVideoFirstBitmap = RecordVideoUtils.getVideoFristImage(videoPath);
        ivCertRecord.setImageBitmap(mVideoFirstBitmap);
        if (!TextUtils.isEmpty(videoPath)) {
            mVideoFile = new File(videoPath);
        }
    }

    @OnClick(R.id.tv_confirm)
    public void confirm() {
        if (mImgFile == null) {
            ToastHelper.show(this, "请先上传头像");
            return;
        }
        if (mVideoFile == null) {
            ToastHelper.show(this, "请先认证视频");
            return;
        }
        if (isVideoLarge(mVideoPath)) {
            ToastHelper.show(this, "认证视频太大");
            return;
        }
        showRunningDialog(R.string.committing);
        String firstPicPath = CommonUtils.saveBitmap(this, mVideoFirstBitmap);
        File videoImg = new File(firstPicPath);
        startTask(CommonBiz.getInstance().prove(mImgFile, mVideoFile, videoImg), baseResponse -> {
            ToastHelper.show(VideoCertActivity.this, baseResponse.msg);
            setResult(RESULT_OK);
            finish();
        });
    }
}
